package com.demo.steammanagement.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.steammanagement.common.QueryBuild;
import com.demo.steammanagement.common.ResultMsg;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.convert.GameAccountConvert;
import com.demo.steammanagement.model.entity.GameAccounts;
import com.demo.steammanagement.model.entity.Games;
import com.demo.steammanagement.model.vo.GameAccountVO;
import com.demo.steammanagement.model.vo.GamesVO;
import com.demo.steammanagement.model.vo.Result;
import com.demo.steammanagement.service.IGameAccountsService;
import com.demo.steammanagement.service.IGamesService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/gameAccount")
public class GameAccountController {

  @Autowired
  private IGameAccountsService gameAccountsService;
  @Autowired
  private IGamesService gamesService;

  @GetMapping("/page")
  public IPage<GameAccounts> page(GameAccountVO vo) {

    if (ObjectUtils.isEmpty(vo)) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }

    Page<GameAccounts> page = new Page<>(vo.getPageNo(), vo.getPageSize());

    return gameAccountsService.page(page, QueryBuild.gamesAccountQueryWrapper(vo));

  }

  @PostMapping("/saveOrUpdate")
  @Transactional
  public Result saveOrUpdate(GameAccountVO vo) throws IOException {
    gameAccountsService.saveOrUpdate(GameAccountConvert.INSTANCE.convertVOToEntity(vo));
    // 获取账号总数
    LambdaQueryWrapper<GameAccounts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(GameAccounts::getGameId, vo.getGameId());
    long count = gameAccountsService.count(lambdaQueryWrapper);
    LambdaQueryWrapper<GameAccounts> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(GameAccounts::getGameId, vo.getGameId()).eq(GameAccounts::getStatus, 0);
    // 可租用数
    long count1 = gameAccountsService.count(queryWrapper);
    Games games = new Games();
    games.setId(vo.getGameId());
    games.setAvailableAccounts((int) count1);
    games.setTotalAccounts((int) count);
    gamesService.updateById(games);
    return Result.success();
  }

  @GetMapping("/get")
  public Result get(GameAccountVO vo) {

    if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }
    return Result.success(gameAccountsService.getById(vo.getId()));

  }

  @GetMapping("/delete")
  public Result delete(GameAccountVO vo) {

    if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }
    return Result.success(gameAccountsService.removeById(vo.getId()));

  }
}
